package com.ht.module.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.constant.RedisConstant;
import com.ht.module.sys.entity.SysDict;
import com.ht.module.sys.entity.SysDictItem;
import com.ht.module.sys.mapper.SysDictMapper;
import com.ht.module.sys.service.SysDictItemService;
import com.ht.module.sys.service.SysDictService;
import com.ht.util.CommMethod;
import com.ht.util.Pager;
import com.ht.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-05-19
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SysDictItemService sysDictItemService;

    @Override
    public List<SysDict> findList(SysDict en) {
        LambdaQueryWrapper<SysDict> query = Wrappers.<SysDict>lambdaQuery()
                .like(StrUtil.isNotEmpty(en.getDictName()), SysDict::getDictName, en.getDictName())
                .eq(StrUtil.isNotEmpty(en.getDictCode()), SysDict::getDictCode, en.getDictCode())
                .eq(StrUtil.isNotEmpty(en.getDictType()), SysDict::getDictType, en.getDictType())
                .orderByDesc(SysDict::getCreateDate);
        return baseMapper.selectList(query);
    }

    @Override
    public Pager<SysDict> findPage(Pager<SysDict> pager, SysDict en) {
        LambdaQueryWrapper<SysDict> query = Wrappers.<SysDict>lambdaQuery()
                .like(StrUtil.isNotEmpty(en.getDictName()), SysDict::getDictName, en.getDictName())
                .eq(StrUtil.isNotEmpty(en.getDictCode()), SysDict::getDictCode, en.getDictCode())
                .eq(StrUtil.isNotEmpty(en.getDictType()), SysDict::getDictType, en.getDictType());
        return baseMapper.selectPage(pager, query);
    }



    @Override
    public List<SysDictItem> getItem(String code) {
        //先从redis中获取码表值
        Map<String, List<SysDictItem>> map = (Map<String, List<SysDictItem>>) redisUtil.get(RedisConstant.DICT_LIST);
        if (ObjectUtil.isEmpty(map)) {
            //全量同步
            return sysDictItemService.fullCacheRedis(code);
        }
        if (ObjectUtil.isEmpty(map.get(code))) {
            //增量同步
            return sysDictItemService.addCacheRedis(map, code);
        }
        return map.get(code);
    }

    @Override
    @Transactional
    public void edit(SysDict sysDict) {
        CommMethod.beanUpdate(sysDict);
        //查询之前的key
        SysDict temp = baseMapper.selectById(sysDict.getId());
        //修改数据库的值
        baseMapper.updateById(sysDict);

        //判断是否需要同步缓存
        if(!StrUtil.equals(temp.getDictCode(),sysDict.getDictCode())){
            //获取缓存中的值
            Map<String, List<SysDictItem>> map = (Map<String, List<SysDictItem>>) redisUtil.get(RedisConstant.DICT_LIST);
            //删除旧的缓存
            map.remove(temp.getDictCode());
            //增量同步
            sysDictItemService.addCacheRedis(map,sysDict.getDictCode());
        }
    }

    @Override
    @Transactional
    public void delById(String id) {
        //查询出当前字典
        SysDict temp = baseMapper.selectById(id);
        //删除字典表
        baseMapper.deleteById(id);
        //删除字典项
        sysDictItemService.remove(new QueryWrapper<SysDictItem>().eq("dict_id",id));
        //删除缓存
        //获取缓存中的值
        Map<String, List<SysDictItem>> map = (Map<String, List<SysDictItem>>) redisUtil.get(RedisConstant.DICT_LIST);
        //删除旧的缓存
        map.remove(temp.getDictCode());
        //同步
        redisUtil.set(RedisConstant.DICT_LIST, map);
    }


}
